/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class="audio_player">
    <div class="bg_info" @click="goRouer">
      <img src="../../assets/images/music_bg.png" alt="" />
      <div class="music_name">
        <span>{{ oneMusicLocal.name }}</span>
        <span>{{ oneMusicLocal.musicName }}</span>
      </div>
    </div>
    <div class="music_icon">
      <img v-show="deviceType !== 'mobile'" src="../../assets/images/play_mode.png" alt="" />
      <img v-show="deviceType !== 'mobile'" @click="previousTrack" src="../../assets/images/control_left.png" alt="" />
      <img v-show="isPlayLocal === 0" @click="playMusic(1)" src="../../assets/images/playBack.svg" alt="" />
      <img v-show="isPlayLocal === 1" @click="playMusic(0)" src="../../assets/images/play_icon.png" alt="" />
      <img v-show="deviceType !== 'mobile'" @click="theNextSong" src="../../assets/images/control_right.png" alt="" />
      <img src="../../assets/images/music_icon.svg" alt="" />
    </div>
    <div v-show="deviceType === 'pc'">
      <img v-show="oneMusicLocal.isCollect" src="../../assets/images/heart_icon.svg" alt="" />
      <img v-show="!oneMusicLocal.isCollect" src="../../assets/images/not_heart_icon.svg" alt="" />
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
  props: {
    deviceType: {
      type: String,
      default: 'mobile',
    },
    listNum: {
      type: Number,
      default: 0,
    },
    musicId: {
      type: Number,
      default: 1,
    },
    isPlay: {
      type: Number,
      default: 0,
    },
    oneMusic: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      oneMusicLocal: this.oneMusic,
      musicIdLocal: this.musicId,
      isPlayLocal: 0,
      musicList: [],
    };
  },
  watch: {
    musicId(newVal) {
      this.musicIdLocal = newVal;
      this.oneMusicLocal = this.musicList[this.musicIdLocal - 1];
    },
    isPlay(newVal) {
      this.isPlayLocal = newVal;
    }
  },
  methods: {
    playMusic(value) {
      this.isPlayLocal = value;
      if (value === 0) {
        this.player.pause();
      } else {
        this.player.play();
      }
      this.player.setCurrentMusicId(this.musicIdLocal);
    },
    goRouer() {
      this.$router.push({
        path: '/musicListDetail',
        query: {
          isPlay: this.isPlayLocal,
          musicId: this.musicIdLocal,
          isPlaying: this.isPlayLocal,
        },
      });
    },
    previousTrack() {
      this.player.pause();
      if (this.musicIdLocal !== 1) {
        this.musicIdLocal--;
        this.$emit('updateMusic', {
          musicId: this.musicIdLocal,
          getOneMusic: this.getOneMusic,
        });
      }
    },
    theNextSong() {
      this.player.pause();
      if (this.musicIdLocal !== this.musicList.length) {
        this.musicIdLocal++;
        if (this.musicIdLocal !== 1) {
          this.$emit('updateMusic', {
            musicId: this.musicIdLocal,
            getOneMusic: this.getOneMusic,
          });
        }
      }
    },
    getOneMusic(obj) {
      this.oneMusicLocal = obj;
      this.player.seekTo(0);
      this.player.play();
      this.isPlayLocal = 1;
    },
  },
  mounted() {
    this.player = this.$musicPlayer;
    const MusicList = sessionStorage.getItem('musicList');
    if (MusicList) {
      this.musicList = JSON.parse(MusicList);
      this.$store.commit('addMusiclist', this.musicList);
    } else {
      this.musicList = this.$store.state.musicList;
    }
    this.player.seekTo(0);
    this.player.pause();
    this.musicIdLocal = this.$props.musicId;
    this.oneMusicLocal = this.musicList[this.musicId - 1];
  },
});
</script>

<style lang="less">
@import "./index.less";
</style>